/*
 * 作者：姚俊
 * 功能：获奖超级检索action
 * 日期：2015-06-26
 */
package admin.action.superSearch;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import admin.action.fruits.Constant;
import admin.service.fruits.AwardService;

import teacher.bean.Award;
import tools.StringDate;

public class AwardSuperAction {
	
	private List<Award> awards;
	private Award award;
	private String date2;
	private int direction;
	//当前页
	private int currentPage;
	//最后一页
	private int endPage;
	private int counts;
	private int flag;
	private int code;
	private String kind;
	private String level;
	private String date;
	
	public List<Award> getAwards() {
		return awards;
	}
	public void setAwards(List<Award> awards) {
		this.awards = awards;
	}
	public Award getAward() {
		return award;
	}
	public void setAward(Award award) {
		this.award = award;
	}
	public String getDate2() {
		return date2;
	}
	public void setDate2(String date2) {
		this.date2 = date2;
	}
	
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getEndPage() {
		return endPage;
	}
	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}
	public int getCounts() {
		return counts;
	}
	public void setCounts(int counts) {
		this.counts = counts;
	}
	public int getDirection() {
		return direction;
	}
	public void setDirection(int direction) {
		this.direction = direction;
	}
	
	public int getFlag() {
		return flag;
	}
	public void setFlag(int flag) {
		this.flag = flag;
	}
	
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	
	
	public String getKind() {
		return kind;
	}
	public void setKind(String kind) {
		this.kind = kind;
	}
	public String getLevel() {
		return level;
	}
	public void setLevel(String level) {
		this.level = level;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String execute()
	{
		//定义按照高级搜索的方式进入list
		this.setFlag(4);
		//
		if(this.getAward()!=null)
		{
			System.out.println("名称为:"+this.getAward().getName());
			
			if(this.getAward().getName()==null||this.getAward().getName().equals(""))
				this.getAward().setName(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getAward().setName(new String(this.getAward().getName().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getAward().getProjectName()==null||this.getAward().getProjectName().equals(""))
				this.getAward().setProjectName(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getAward().setProjectName(new String(this.getAward().getProjectName().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getAward().getEmpowerUnit()==null||this.getAward().getEmpowerUnit().equals(""))
				this.getAward().setEmpowerUnit(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getAward().setEmpowerUnit(new String(this.getAward().getEmpowerUnit().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getAward().getPeople()==null||this.getAward().getPeople().equals(""))
				this.getAward().setPeople(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getAward().setPeople(new String(this.getAward().getPeople().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getAward().getDate()!=null)
				this.getAward().setDate(
						StringDate.StringToDate(
								StringDate.DateToString(
										this.getAward().getDate())));
			
		}
		/**
		 * laohe; 排序
		 */
		List<String> orders=new ArrayList<String>();
		if(this.getKind()!=null&&!(this.getKind().equals("")))
		{
			orders.add(this.getKind());
		}
		if(this.getLevel()!=null&&!(this.getLevel().equals("")))
		{
			orders.add(this.getLevel());
		}
		if(this.getDate()!=null&&!(this.getDate().equals("")))
		{
			orders.add(this.getDate());
		}
		//System.out.println("kkkk"+orders.size());
		// 排序结束
		//当前页为零的时候，把当前页变成1
				if(this.getCurrentPage()==0)
					this.setCurrentPage(1);
				//System.out.println("fdsafdsafdsafdsafdsa");
				
				int add=0;
				//最后一页
				if(new AwardService().getAwardCountByDirection(
						this.getAward(), this.getDate2(),this.getDirection(),Constant.state)%Constant.pageSize!=0)
				{
					add=1;
				}

				this.setEndPage(
						new AwardService().getAwardCountByDirection(
								this.getAward(), this.getDate2(),this.getDirection(),Constant.state)/Constant.pageSize+add);
				this.setCounts(new AwardService().getAwardCountByDirection(
						this.getAward(), this.getDate2(), this.getDirection(),Constant.state));
		
		this.setAwards(
				new AwardService().getSegregateAwardByDirection(
						this.getAward(), this.getDate2(),this.getDirection(),(this.getCurrentPage()-1)*Constant.pageSize+1,Constant.pageSize,Constant.state,orders));
		return "succ";
	}

}
